<template>
	<view v-if="pageShow" class="container">
		<scroll-view scroll-y="true" :refresher-threshold="50" :refresher-background="'#FDFDFD'"
			:refresher-enabled="true" :refresher-triggered="isRefreshing" @refresherrefresh="refresherrefresh"
			@scrolltolower="loadMore" style="height: 100vh">
			<view class="header">
				<view class="header-img">
					<image :src="serviceInfo.logo" mode="aspectFill"></image>
				</view>
				<view class="header-name">
					<text>{{serviceInfo.name}}</text>
				</view>
				<view class="header-type">
					<text class="type">{{getServiceType(serviceInfo.type)}}</text>
					<text class="type" style="margin-left: 24rpx">保证金{{serviceInfo.cash_deposit}}</text>
				</view>
				<view class="header-local">
					地址：{{serviceInfo.city}}
				</view>
			</view>
			<view class="service-main">
				<view class="solid-bottom">
					<u-tabs :list="tabList" @click="tabClick" :current="tabCurrent" :scrollable="true" :activeStyle="{
    							color: '#0177FD', fontWeight: 'bold',fontSize: '36rpx'}" :inactiveStyle="{
    							color: '#666666',fontSize: '36rpx', fontWeight: 'bold'}" :itemStyle="{height: '110rpx'}">
					</u-tabs>
				</view>
				<view v-if="tabCurrent === 0" class="shop-list">
					<view class="shop-item" v-for="(item, index) in serviceShopList" :key="index"
						@click="goShopDetail(item)">
						<view class="shop-img">
							<image :src="item.pic[0]" mode="aspectFill"></image>
						</view>
						<view class="shop-info">
							<text class="name">{{item.name}}</text>
							<text class="price">{{item.price == 0?'面议': '¥' + item.price}}</text>
						</view>
					</view>
					<uni-load-more :status="shopStatus"></uni-load-more>
				</view>
				<view v-if="tabCurrent === 1" class="service-des">
					<view v-if="serviceInfo.introduce" class="des">
						{{serviceInfo.introduce}}
					</view>
					<view v-if="!serviceInfo.introduce" class="empty-wrap">
						<image class="empty-img"
							src="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/coupon/contentVanshi.png">
						</image>
						<text class="tips">暂无简介</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="now-contact" @click="openKefu">
			<image
				src="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/enterprise-service/serviceImg/now_service_icon.png">
			</image>
		</view>
		
		<!-- 底部固定按钮-->
		<view class="fixed-btn" v-if="loginBtn">
			<view class="bottom-btn">
				<view class="btn-text">
					<text>
						登录查看更多优质内容
					</text>
					<text>
						新用户登录即享好礼~
					</text>
				</view>
				<view class="btn" @click="goWxLogin">
					马上登录
				</view>
			</view>
		</view>
		<!-- 底部固定按钮 end-->
	</view>
</template>

<script>
	import shopDetail from "@/api/shopDetail";
	import personalApi from '@/api/personal.js'
	export default {
		data() {
			return {

				//分页参数
				pageShow: false,
				page: 1,
				pageSize: 10,
				shopStatus: 'nomore',
				isRefreshing: false,
				//主要数据
				serviceId: '',
				serviceObj: {},
				serviceInfo: {},
				serviceShopList: [],

				//导航栏切换
				tabCurrent: 0,
				tabList: [{
						id: 0,
						name: '服务介绍'
					},
					{
						id: 1,
						name: '公司简介'
					}
				],
				
				inviteCode: '',
				loginBtn: true
			};
		},
		onLoad(e) {
			this.serviceId = e.id
			this.inviteCode = uni.getStorageSync('userInfo').invite_code
			console.log('获取自己的邀请码', this.inviteCode)
			if(e.invite_code) {
				uni.setStorageSync('inviteCode', e.invite_code)
				console.log('我已经保存了inviteCode', e.invite_code)
			}
			this.getDetail()
		},
		onShow() {
			if(uni.getStorageSync('token')) {
				this.loginBtn = false
			}else {
				this.loginBtn = true
			}
		},
		methods: {
			async getWeChatClick() {
				const pages = getCurrentPages()
				let currentRoute  = pages[pages.length - 1].route
				let obj = {
					type: 3,
					m_id: 9,
					describe: this.serviceInfo.name,
					url: currentRoute,
					code: 'WH1'
				}
				let res = await personalApi.getWechatClick(obj)
			},
			//分享微信好友
			onShareAppMessage() {
				return {
					title: this.serviceInfo.name,
					path: '/pagesShop/serviceDetail/serviceDetail?invite_code=' + this.inviteCode + "&id=" + this.serviceId + "&pathType=2"
				}
			},
			
			//分享微信朋友圈
			onShareTimeline() {
				return {
					title: this.serviceInfo.name,
					query: 'id=' + this.serviceId + "&invite_code=" + this.inviteCode + "&pathType=2"
				}
			},
			
			//去往登录页
			goWxLogin() {
				uni.redirectTo({
					url: "/pagesLogin/wxLogin/wxLogin?id=" + this.serviceId + "&pathType=2"
				})
			},
			//获取不同的服务商类型
			getServiceType(type) {
				let text = "";
				switch (type) {
					case 1:
						text = "官方服务商";
						break;
					case 2:
						text = "金牌服务商";
						break;
					case 3:
						text = "银牌服务商";
						break;
					case 4:
						text = "铜牌服务商";
						break;
					case 5:
						text = "普通服务商";
						break;
				}
				return text;
			},

			//获取服务商详情
			async getDetail() {
				let obj = {
					page: this.page,
					pageSize: this.pageSize,
					id: this.serviceId
				}
				let res = await shopDetail.getServiceDetail(obj)
				if (res.code === 1) {
					this.serviceObj = res.data
					this.serviceInfo = res.data.info
					this.getWeChatClick()
					let arr = []
					if (this.page == 1) {
						this.serviceShopList = []
					}
					let newList = res.data.serviceshoplist.data
					if (newList.length >= this.pageSize) {
						this.shopStatus = 'more'
					} else {
						this.shopStatus = 'nomore'
					}
					let arr1 = arr.concat(newList)
					arr1.forEach(val => {
						let obj = {
							id: val.id,
							name: val.name,
							pic: val.pic.split(','),
							price: val.price,
						}
						this.serviceShopList.push(obj)
					})
					this.isRefreshing = false
					this.pageShow = true
				} else {
					this.$common.msgShow(res.msg)
				}
			},
			//导航栏切换事件
			tabClick(e) {
				console.log(e)
				this.tabCurrent = e.index
			},
			//下拉刷新
			refresherrefresh() {
				this.isRefreshing = true;
				this.page = 1
				this.getDetail()
			},
			//上拉加载
			loadMore() {
				if (this.shopStatus == 'more') {
					this.page += 1
					this.getDetail()
				}
			},

			//跳转上商品详情
			goShopDetail(item) {
				uni.navigateTo({
					url: "/pagesShop/shopDetail/shopDetail?id=" + item.id
				})
			},
			
			//联系服务商
			openKefu() {
				if(!uni.getStorageSync('token')) {
					this.$common.handleToken()
					return
				}
				if(uni.getStorageSync('userInfo').id == this.serviceInfo.user_id) {
					this.$common.msgShow('您不能与自己沟通')
					return
				}
				uni.setStorageSync("to_zim_userid", this.serviceInfo.user_id);
				wx.requestSubscribeMessage({
				  tmplIds: ['FDa3cltqtpSJ-1enjsx3vym6nRR8r1CDMpJQkUpr2DQ', 'maUnBJPtl6zy5bnMvtfPHDJtJbSkwhywbg7FUjoM3Vc'],
				  success (res) {
						console.log(res)
				  }
				})
				uni.navigateTo({
					url: '/pages/message/chat?id=' + this.serviceInfo.user_id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "serviceDetail";
</style>